TMacros = AceLibrary("AceAddon-2.0"):new("AceEvent-2.0")
TMacros.ToolTip = CreateFrame("GameTooltip", "TurtleMacrosTip", nil, "GameTooltipTemplate")

TMacros.GCD = 1.5
TMacros.NameText = TurtleMacrosTipTextLeft1
TMacros.RankText = TurtleMacrosTipTextRight1
TMacros.AutoShootSlot = 23

function TMacros:OnInitialize()
    TMacros:RegisterEvents()
end

function TMacros:RegisterEvents()
    self:RegisterEvent("GOSSIP_SHOW")
    self:RegisterEvent("GOSSIP_CLOSED")
    self:RegisterEvent("QUEST_ACCEPT_CONFIRM")
    self:RegisterEvent("QUEST_COMPLETE")
    self:RegisterEvent("QUEST_DETAIL")
    self:RegisterEvent("QUEST_FINISHED")
    self:RegisterEvent("QUEST_GREETING")
end

SLASH_PLACE1 = "/place"
SLASH_SHOOT1 = "/shoot"
SLASH_STOPSHOOT1 = "/stopshoot"
SLASH_FEED1 = "/feed"
SLASH_SPELLONENEMY1 = "/spellonenemy"
SLASH_SPELLONHELP1 = "/spellonhelp"

SlashCmdList["PLACE"] = function(msg)
    TMacros:PlaceActionToSlot(msg)
end

SlashCmdList["SHOOT"] = function(msg)
    TMacros:StartShoot()
end

SlashCmdList["STOPSHOOT"] = function(msg)
    TMacros:StopShoot()
end

SlashCmdList["FEED"] = function(msg)
    TMacros:FeedPet(msg)
end

SlashCmdList["SPELLONENEMY"] = function(msg)
    TMacros:CastOnEnemy(msg)
end

SlashCmdList["SPELLONHELP"] = function(msg)
    TMacros:CastOnFriend(msg)
end

function TMacros:PlaceActionToSlot(msg)
    local trim = string.trim(msg)
    local tab = self:StringSplit(trim)
    local slot, Spell, Rank = tonumber(tab[1]), tab[2], tab[3]
    if not slot or not Spell then return end
    self:PlaceAction(slot, Spell, Rank)
end

function TMacros:StartShoot()
    local slot = self.AutoShootSlot
    self:PlaceAction(slot, "自动射击")
    if IsActionInRange(slot) == 1 and not IsAutoRepeatAction(slot) then
        UseAction(slot)
    end
end

function TMacros:StopShoot()
    local slot = self.AutoShootSlot
    self:PlaceAction(slot, "自动射击")
    if IsAutoRepeatAction(slot) then UseAction(slot) end
end

-- string.split may be blocked by some reasons by another add on
function TMacros:StringSplit(Source)
    local Table = {}
    for result in string.gmatch(Source, "[^%s]+") do 
        table.insert(Table, result)
    end

    return Table
end

function TMacros:FeedPet(msg)
    local names = self:StringSplit(string.trim(msg))
    if names[1] and type(names[1] == "string") then
        self:DropItemOnUnit(names[1], "pet")
    end
end

function TMacros:CastOnFriend(msg)
    local table = self:StringSplit(string.trim(msg))
    if table[1] then
        self:CastSpellOptionTarget(table[1], true, true)
    end
end

function TMacros:CastOnEnemy(msg)
    local table = self:StringSplit(string.trim(msg))
    if table[1] then
        self:CastSpellOptionTarget(table[1])
    end
end

function TMacros:BuyHolySharpenStone()
    local title = "长夜未尽"
    local index = self:NPCGossipHasAvailableQuest(title)
    if index then 
        SelectGossipAvailableQuest(index)
        AcceptQuest()
    end

    index = self:NPCGossipHasActiveQuest(title)
    if index then
        SelectGossipActiveQuest(index)
        CompleteQuest()
        GetQuestReward(1)
        CloseGossip()
    end
end

function TMacros:PrintTable(table)
    if type(table) ~= "table" then 
        print(v)
        return 
    end
    
    for i, v in pairs(table) do 
        if (type(v) == table) then
            self:PrintTable(v)
        else
            print(v)
        end
    end
end